package com.yuanfengerp.basicInfoManage.addressBase.servicer.impl;

import com.yuanfengerp.basicInfoManage.addressBase.dao.IAddressLibDao;
import com.yuanfengerp.basicInfoManage.addressBase.model.AddressLibrary;
import com.yuanfengerp.basicInfoManage.addressBase.servicer.IAddressService;
import com.yuanfengerp.publicUtil.Result;
import com.yuanfengerp.publicUtil.SnowflakeIdWorker;
import io.lettuce.core.api.reactive.BaseRedisReactiveCommands;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service

public class IAddressServiceImpl implements IAddressService {
    @Autowired
    private IAddressLibDao addressLibDao;

    @Override
    public Result<List<AddressLibrary>> selectAllAddress( Map<String , Object > params) {
        params.put("pageSize",((int) params.get("pageNum")-1) * (int) params.get("pageSize"));
        List<AddressLibrary> addressLibraries = addressLibDao.selectAddressLib(params);
        System.out.println("====================================================================");
        System.out.println(addressLibraries);
        System.out.println("====================================================================");
//        int count = addressLibraries.size();
        if (!addressLibraries.isEmpty()) {
            return Result.success(addressLibraries,addressLibDao.count() );
        } else {
            return Result.failure(400, "没有找到地址库");
        }
    }

    /**
     * 新增地址
     *
     * @param addressLibrary 新增地址
     * @return Result
     */
    @Override
    public Result<AddressLibrary> insertAddress(AddressLibrary addressLibrary) {
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0);
        addressLibrary.setAddress_Library_ID(idWorker.nextId());
        System.out.println(idWorker);
        System.out.println(idWorker.toString());
        int i = addressLibDao.insertAddressLib(addressLibrary);
        if (i > 0) {
            return Result.success(addressLibrary);
        } else {
            return Result.failure(400, "新增失败");
        }
    }

    /**
     * 查看地址
     *
     * @param addressLibraryId 地址名称
     * @return Result
     */
    @Override
    public Result<AddressLibrary> selectAddressById(String addressLibraryId) {
        if (addressLibDao.selectAddressByName(addressLibraryId) != null) {
            return Result.success(addressLibDao.selectAddressByName(addressLibraryId));
        } else {
            return Result.failure(400, "查看失败");
        }

    }

    /**
     * 修改地址
     *
     * @param addressLibrary 修改地址
     * @return Result
     */
    @Override
    public Result<AddressLibrary> updateAddress(AddressLibrary addressLibrary) {
        int i = addressLibDao.updateAddressByID(addressLibrary);
        if (i > 0) {
            return Result.success(addressLibrary);
        } else {
            return Result.failure(400, "修改失败");
        }
    }
}
